package com.nari.ysphd.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class CustomLayout extends ViewGroup {

    private static final String TAG = CustomLayout.class.getSimpleName();

    private ViewGroup leftChildView, rightChildView;
    private int leftChildWidth;
    private int widthRatio = 16;
    private int heightRatio = 9;
    private int displayWidth;
    private int displayHeight;

    public CustomLayout(Context context) {
        this(context, null);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        if (childCount != 2) {
            throw new IllegalStateException("Children num must equal 2");
        } else {
            leftChildView = (ViewGroup) getChildAt(0);
            rightChildView = (ViewGroup) getChildAt(1);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);

        displayWidth = measureWidth;
        displayHeight = measureHeight;

        if ((measureWidth * 1.0 / measureHeight) > (widthRatio * 1.0 / heightRatio)) {
            int width = MeasureSpec.getSize(heightMeasureSpec) * widthRatio / heightRatio;
            int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            Log.d(TAG, "onMeasure: 宽屏 w*h==>" + width + "*" + measureHeight);
            displayWidth = width;
            super.onMeasure(widthSpec, heightMeasureSpec);
        } else {
            int height = MeasureSpec.getSize(widthMeasureSpec) * heightRatio / widthRatio;
            int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            Log.d(TAG, "onMeasure: 窄屏 w*h==>" + measureWidth + "*" + height);
            displayHeight = height;
            super.onMeasure(widthMeasureSpec, heightSpec);
        }
        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        Log.d(TAG, "onMeasure: w ==" + displayWidth + " h ==" + displayHeight);

        leftChildWidth = displayWidth - rightChildView.getMeasuredWidth() / 2;

        int count = leftChildView.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = leftChildView.getChildAt(i);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(leftChildWidth, MeasureSpec.AT_MOST);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }

        for (int i = 0; i < rightChildView.getChildCount(); i++) {
            View child = rightChildView.getChildAt(i);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), MeasureSpec.EXACTLY);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }


        int width = 0;
        int height = 0;

        int lChildHeight = leftChildView.getMeasuredHeight();
        int rChildHeight = rightChildView.getMeasuredHeight();
        int lChildWidth = leftChildView.getMeasuredWidth();
        int rChildWidth = rightChildView.getMeasuredWidth();


        setMeasuredDimension(displayWidth, Math.max(measureHeight,displayHeight));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int lChildHeight = leftChildView.getMeasuredHeight();
        int rChildHeight = rightChildView.getMeasuredHeight();
        int lChildWidth = leftChildView.getMeasuredWidth();
        int rChildWidth = rightChildView.getMeasuredWidth();


        leftChildView.layout(0,
                0,
                leftChildWidth,
                lChildHeight);
        rightChildView.layout(leftChildWidth,
                0,
                displayWidth,
                rChildHeight);
    }
}
